package com.lingo.mgr.controller;

import com.alibaba.fastjson.JSONObject;

import com.lingo.common.annotation.RepeatSubmit;
import com.lingo.common.core.controller.BaseController;
import com.lingo.common.core.domain.R;
import com.lingo.common.core.page.TableDataInfo;
import com.lingo.mgr.domain.bo.ChatSessionBo;
import com.lingo.mgr.domain.bo.ChatSessionStartBo;
import com.lingo.mgr.domain.vo.ChatSessionMessageVo;
import com.lingo.mgr.domain.vo.ChatSessionStartVo;
import com.lingo.mgr.service.chatSession.IChatSessionService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import javax.servlet.http.HttpServletRequest;

/**
 * 聊天消息会话
 *
 * @author ruoyi
 * @date 2023-08-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/chatSession")
@Slf4j
public class ChatSessionController extends BaseController {

    private final IChatSessionService iChatSessionService;

    /**
     * 查询聊天消息会话列表
     */
//    @SaCheckPermission("chatSession:session:list")
    @PostMapping("/list")
    public TableDataInfo<ChatSessionStartVo> list(@RequestBody ChatSessionBo bo) {
        return iChatSessionService.queryPageList(bo);
    }


    @PostMapping("/recordBySessionId")
    public TableDataInfo<ChatSessionMessageVo> recordBySessionId(@RequestBody ChatSessionBo bo) {
        return iChatSessionService.recordBySessionId(bo);
    }


    @PostMapping("/start")
    public R<ChatSessionStartVo> start(@RequestBody @Validated ChatSessionStartBo bo, HttpServletRequest request) {
        log.info("【新版开始对话】请求参数: {}", bo);

        return R.ok(iChatSessionService.start(bo,request));
    }

    @PostMapping("/reStart")
    public R<ChatSessionStartVo> reStart(@RequestBody @Validated ChatSessionStartBo bo,HttpServletRequest request) {
        log.info("【新版重试对话】请求参数: {}", bo);

        return R.ok(iChatSessionService.reStart(bo,request));
    }

    @GetMapping("/sessionId={sessionId}&dataId={dataId}")
    public SseEmitter sse(@PathVariable Long sessionId, @PathVariable Long dataId) {
        log.info("【新版查询对话】请求参数, sessionId: {}, dataId: {}", sessionId, dataId);
        return iChatSessionService.sse(sessionId, dataId);
    }

    @PostMapping("/removeById")
    @RepeatSubmit
    public R<Void> removeById(@RequestBody ChatSessionBo bo) {
        return toAjax(iChatSessionService.deleteById(bo.getSessionId()));
    }

    @GetMapping("/getSearchPrompt")
//    @RepeatSubmit
    public R<JSONObject> getSearchPrompt() {
        return R.ok(iChatSessionService.getSearchPrompt());
    }


    @GetMapping("/getSimilarPrompt")
//    @RepeatSubmit
    public R<JSONObject> getSimilarPrompt(@RequestParam("sessionId")Long sessionId) {
        return R.ok(iChatSessionService.getSimilarPrompt(sessionId));
    }
}
